package com.hzqy.service.behaviorControl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzqy.persistence.dao.behaviorControl.BehaviorControlDao;
import com.hzqy.service.pms.PmsServiceBean;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.behaviorControl.IndustryGroupSchedulesVo;
import com.hzqy.web.vo.behaviorControl.IndustryOrgContentSchedulesVo;
import com.hzqy.web.vo.behaviorControl.IndustrySchedulesVo;

@Service
public class BehaviorControlServiceBean {

	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private BehaviorControlDao behaviorControlDao;
	@Resource
	private PmsServiceBean pmsServiceBean;
	
	public Map<String, Object> forcedSwitchingServeridList(IndustryOrgContentSchedulesVo industryOrgContentSchedulesVo,PmsUserVo userVo,int page, int rows) {
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
			industryOrgContentSchedulesVo.setF_iocs_orgIds(orgIds);
		}
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = behaviorControlDao.forcedSwitchingServeridListCount(industryOrgContentSchedulesVo);
		map.put("total", rowCount);
		if(rowCount > 0 && page > 0 && rows > 0) {
			map.put("firstResult", (page-1)*rows);
			map.put("maxResults", rows);
			map.put("orgName", industryOrgContentSchedulesVo.getOrgName());
			map.put("f_iocs_orgId", industryOrgContentSchedulesVo.getF_iocs_orgId());
			map.put("groupSchedulesName", industryOrgContentSchedulesVo.getGroupSchedulesName());
			map.put("f_iocs_serverid", industryOrgContentSchedulesVo.getF_iocs_serverid());
			map.put("f_iocs_orgIds", industryOrgContentSchedulesVo.getF_iocs_orgIds());
			map.put("seachName", industryOrgContentSchedulesVo.getSeachName());
			List<IndustryOrgContentSchedulesVo> results = behaviorControlDao.forcedSwitchingServeridList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<IndustryOrgContentSchedulesVo>());
		}
		return map;
	}
	
	
	/**
	 * 强切分组列表获取
	 * @param industryGroupSchedulesVo
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public Map<String,Object> selectGroupList(IndustryGroupSchedulesVo industryGroupSchedulesVo, PmsUserVo userVo,int curPage,
			int pageSize) {
		String address = String.format("^%s-%s-%s$", 
				StringUtils.isNotEmpty(industryGroupSchedulesVo.getArea_province()) ? industryGroupSchedulesVo.getArea_province() : "[0-9]{1,2}",
				StringUtils.isNotEmpty(industryGroupSchedulesVo.getArea_city()) ? industryGroupSchedulesVo.getArea_city() : "[0-9]{1,2}",
				StringUtils.isNotEmpty(industryGroupSchedulesVo.getArea_county()) ? industryGroupSchedulesVo.getArea_county() : "[0-9]{1,2}"
		);
		industryGroupSchedulesVo.setAddress(address);
		
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了 
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
			industryGroupSchedulesVo.setF_igs_orgIds(orgIds);
		}
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = behaviorControlDao.findGroupCount(industryGroupSchedulesVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("address", industryGroupSchedulesVo.getAddress());
			map.put("orgName", industryGroupSchedulesVo.getOrgName());
			map.put("f_igs_name", industryGroupSchedulesVo.getF_igs_name());
			map.put("f_igs_orgId", industryGroupSchedulesVo.getF_igs_orgId());
			map.put("f_igs_status", industryGroupSchedulesVo.getF_igs_status());
			map.put("f_igs_orgIds", industryGroupSchedulesVo.getF_igs_orgIds());
			List<IndustryGroupSchedulesVo> results = behaviorControlDao.findGroupList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<IndustryGroupSchedulesVo>());
		}
		return map;
	}


	public Map<String, Object> bingOrgServeridGroup(IndustryOrgContentSchedulesVo industryOrgContentSchedulesVo) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		int flag = 0;
		if(industryOrgContentSchedulesVo.getF_iocs_igsId() == 0){
			//未绑定，直接添加
			industryOrgContentSchedulesVo.setF_iocs_igsId(industryOrgContentSchedulesVo.getfIocsIgsId());
			flag = behaviorControlDao.bingOrgServeridGroup(industryOrgContentSchedulesVo);
		}else{
			//已绑定，先删除
			flag = behaviorControlDao.deleteBindOrgServeridGroup(industryOrgContentSchedulesVo);
			if(flag == 0){
				//删除失败，不做操作
			}else{
				//删除成功，进行添加操作
				industryOrgContentSchedulesVo.setF_iocs_igsId(industryOrgContentSchedulesVo.getfIocsIgsId());
				flag = behaviorControlDao.bingOrgServeridGroup(industryOrgContentSchedulesVo);
			}
		}
		
		resultMap.put("flag", flag);
		return resultMap;
	}
	
	public Map<String, Object> onlineStatisticsList(IndustryOrgContentSchedulesVo industryOrgContentSchedulesVo,PmsUserVo userVo, int page, int rows) {
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了 
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
			industryOrgContentSchedulesVo.setF_iocs_orgIds(orgIds);
		}
		Map<String,Object> map = new HashMap<String, Object>();
		String startDate = industryOrgContentSchedulesVo.getYear() +"-"+industryOrgContentSchedulesVo.getMonth()+"-01 00:00:00";
		String endDate = industryOrgContentSchedulesVo.getYear() +"-"+industryOrgContentSchedulesVo.getMonth()+"-31 23:59:59";
		map.put("firstResult", (page-1)*rows);
		map.put("maxResults", rows);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("orgName", industryOrgContentSchedulesVo.getOrgName());
		map.put("f_iocs_orgId", industryOrgContentSchedulesVo.getF_iocs_orgId());
		map.put("groupSchedulesName", industryOrgContentSchedulesVo.getGroupSchedulesName());
		map.put("f_iocs_serverid", industryOrgContentSchedulesVo.getF_iocs_serverid());
		map.put("f_iocs_orgIds", industryOrgContentSchedulesVo.getF_iocs_orgIds());
		
		Integer rowCount = behaviorControlDao.onlineStatisticsListCount(map);
		map.put("total", rowCount);
		if(rowCount > 0 && page > 0 && rows > 0) {
			List<IndustryOrgContentSchedulesVo> results = behaviorControlDao.onlineStatisticsList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<IndustryOrgContentSchedulesVo>());
		}
		
		return map;
	}
	
	public int batchDeleteGroup(String ids) {
		int flag = 0;
		String[] idsArr = ids.split(",");
		for(int i=0;i<idsArr.length;i++) {
			flag += behaviorControlDao.deleteGroup(Integer.valueOf(idsArr[i]));
		}
		return flag;
	}
	
	public int insertGroupSchedules(IndustryGroupSchedulesVo industryGroupSchedulesVo) {
		int flag = 0;
		if(industryGroupSchedulesVo==null || industryGroupSchedulesVo.getF_igs_name()==null) {
			flag = -1;
		}
		List<IndustrySchedulesVo> industrySchedulesList = industryGroupSchedulesVo.getIndustrySchedulesVoList();
		if(flag ==0) {
			flag = behaviorControlDao.insertGroupSchedules(industryGroupSchedulesVo);
			int f_igs_id = industryGroupSchedulesVo.getF_igs_id();
			if(industrySchedulesList!=null){
				for(int i=0;i<industrySchedulesList.size();i++) {
					IndustrySchedulesVo industrySchedulesVo = new IndustrySchedulesVo();
					industrySchedulesVo = industrySchedulesList.get(i);
					if(industrySchedulesVo.getF_is_name()!=null&&industrySchedulesVo.getF_is_name()!=""){
						industrySchedulesVo.setF_is_igsId(f_igs_id);
						flag = behaviorControlDao.insertSchedules(industrySchedulesVo);
					}
				}
			}
		}
		
		return flag;
	}
	
	/**
	 * 根据id查询组计划详情
	 * @param f_cc_id
	 * @return
	 */
	public IndustryGroupSchedulesVo selectGroupSchedules(int f_igs_id) {
		return behaviorControlDao.selectGroupSchedules(f_igs_id);
	}
	
	public List<IndustryGroupSchedulesVo> selectSchedulesList(IndustrySchedulesVo industrySchedulesVo) {
		List<IndustryGroupSchedulesVo> results = behaviorControlDao.selectSchedulesList(industrySchedulesVo);
		return results;
	}
	
	public int updateGroupSchedules(IndustryGroupSchedulesVo industryGroupSchedulesVo) {
		int flag = 0;
		if(industryGroupSchedulesVo==null || industryGroupSchedulesVo.getF_igs_name()==null) {
			flag = -1;
		}
		int f_igs_id = industryGroupSchedulesVo.getF_igs_id();
		List<IndustrySchedulesVo> industrySchedulesList = industryGroupSchedulesVo.getIndustrySchedulesVoList();
		if(flag ==0) {
			behaviorControlDao.updateGroupSchedules(industryGroupSchedulesVo);
			flag = behaviorControlDao.deleteSchedules(f_igs_id);
			if(industrySchedulesList!=null){
				for(int i=0;i<industrySchedulesList.size();i++) {
					IndustrySchedulesVo industrySchedulesVo = new IndustrySchedulesVo();
					industrySchedulesVo = industrySchedulesList.get(i);
					if(industrySchedulesVo.getF_is_name()!=null&&industrySchedulesVo.getF_is_name()!=""){
						industrySchedulesVo.setF_is_igsId(f_igs_id);
						flag = behaviorControlDao.insertSchedules(industrySchedulesVo);
					}
				}
			}
		}
		
		return flag;
	}
	
	/**
	 * 批量绑定或者解绑的方法
	 * @param industryOrgContentSchedulesVoList
	 * @param deleteOrBindType    是绑定操作还是解绑操作：0代表绑定；1代表解绑
	 * @return
	 */
	public Map<String, Object> bingOrDeleteOrgServeridGroups(String industryGroupSchedulesVoString,Integer fIocsIgsId,Integer deleteOrBindType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int flag = 0;
		
		List<IndustryOrgContentSchedulesVo> industryOrgContentSchedulesVoList = new ArrayList<IndustryOrgContentSchedulesVo>();
		JSONArray jsonArray = JSONArray.parseArray(industryGroupSchedulesVoString);
		
		for(int i=0; i<jsonArray.size(); i++){
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			IndustryOrgContentSchedulesVo industryOrgContentSchedulesVo = new IndustryOrgContentSchedulesVo();
			
			industryOrgContentSchedulesVo.setF_iocs_orgId(Integer.valueOf(String.valueOf(jsonObject.get("f_iocs_orgId"))).intValue());
			industryOrgContentSchedulesVo.setF_iocs_serverid(String.valueOf(jsonObject.get("f_iocs_serverid")));
			industryOrgContentSchedulesVo.setF_iocs_igsId(Integer.valueOf(String.valueOf(jsonObject.get("f_iocs_igsId"))).intValue());
			industryOrgContentSchedulesVo.setfIocsIgsId(fIocsIgsId);
			industryOrgContentSchedulesVoList.add(industryOrgContentSchedulesVo);
		}
		
		if(deleteOrBindType==0){
			
			//批量新增绑定的数据
			for (IndustryOrgContentSchedulesVo industryOrgContentSchedulesVo : industryOrgContentSchedulesVoList) {
				if(industryOrgContentSchedulesVo.getF_iocs_igsId() == 0){
					//未绑定，直接添加
					industryOrgContentSchedulesVo.setF_iocs_igsId(industryOrgContentSchedulesVo.getfIocsIgsId());
					flag = behaviorControlDao.bingOrgServeridGroup(industryOrgContentSchedulesVo);
				}else{
					//已绑定，先删除
					flag = behaviorControlDao.deleteBindOrgServeridGroup(industryOrgContentSchedulesVo);
					if(flag == 0){
						//删除失败，不做操作
					}else{
						//删除成功，进行添加操作
						industryOrgContentSchedulesVo.setF_iocs_igsId(industryOrgContentSchedulesVo.getfIocsIgsId());
						flag = behaviorControlDao.bingOrgServeridGroup(industryOrgContentSchedulesVo);
					}
				}
			}
		}else if(deleteOrBindType==1){
			//批量删除绑定的数据
			flag = behaviorControlDao.batchDeleteBindOrgServeridGroup(industryOrgContentSchedulesVoList);
		}
		
		resultMap.put("flag", flag);
		return resultMap;
	}
	
	/**
	 * 导出活跃统计数据
	 * @param industryOrgContentSchedulesVo
	 * @param userVo
	 * @return
	 */
	public Map<String, Object> onlineStatisticsExcelList(IndustryOrgContentSchedulesVo industryOrgContentSchedulesVo,PmsUserVo userVo) {
		//父级组织id为0是根组织,有最高权限 否则必须使用组织本身id覆盖companyid,就看不到别的组织的内容了 
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
			industryOrgContentSchedulesVo.setF_iocs_orgIds(orgIds);
		}
		Map<String,Object> map = new HashMap<String, Object>();
		
		String startDate = industryOrgContentSchedulesVo.getYear() +"-"+industryOrgContentSchedulesVo.getMonth()+"-01 00:00:00";
		String endDate = industryOrgContentSchedulesVo.getYear() +"-"+industryOrgContentSchedulesVo.getMonth()+"-31 23:59:59";
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("orgName", industryOrgContentSchedulesVo.getOrgName());
		map.put("f_iocs_orgId", industryOrgContentSchedulesVo.getF_iocs_orgId());
		map.put("groupSchedulesName", industryOrgContentSchedulesVo.getGroupSchedulesName());
		map.put("f_iocs_serverid", industryOrgContentSchedulesVo.getF_iocs_serverid());
		map.put("f_iocs_orgIds", industryOrgContentSchedulesVo.getF_iocs_orgIds());
		
		List<IndustryOrgContentSchedulesVo> results = behaviorControlDao.onlineStatisticsExcelList(map);
		
		if(results!=null&&!results.isEmpty()){
			map.put("rows", results);
		}else{
			map.put("rows", new ArrayList<IndustryOrgContentSchedulesVo>());
		}
		
		return map;
	}
	
}
